//
//  UITextView+CXTextView.m
//  医讯通
//
//  Created by Cynic on 2018/6/15.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "UITextView+CXTextView.h"

@implementation UITextView (CXTextView)
static void *hoderindentifier;
static void *cx_textVieweventsBlock;
static void *maxKey;

-(UILabel *)PlaceHolder{
    return objc_getAssociatedObject(self,&hoderindentifier);
}

#pragma mark--添加uitextview placeholder关联属性 默认设置max值100
-(void)loadplaceholder{
    self.max = 100;
    self.PlaceHolder = [[UILabel alloc] initWithFrame:CGRectZero];
    self.scrollEnabled = NO;
    [self addSubview:self.PlaceHolder];
    self.PlaceHolder.textColor = [UIColor lightGrayColor];
    self.PlaceHolder.font = self.font;
    [self.PlaceHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        //        make.top.mas_equalTo(8);
        make.right.mas_equalTo(-1);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(16);
    }];
}

-(void)autoCXChange{
    [self cx_eventsChangeByHandler:^{
        
    }];
}

-(void)setPlaceHolder:(UILabel *)PlaceHolder{
    objc_setAssociatedObject(self, &hoderindentifier, PlaceHolder,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSInteger)max{
    return [objc_getAssociatedObject(self, &maxKey) integerValue];
}

-(void)setMax:(NSInteger)max{
    objc_setAssociatedObject(self, &maxKey, [NSNumber numberWithInteger:max],OBJC_ASSOCIATION_ASSIGN);
}





-(void)setCX_TextViewEventsBlock:(TextViewEventsBlock)CX_TextViewEventsBlock{
    objc_setAssociatedObject(self, &cx_textVieweventsBlock, CX_TextViewEventsBlock, OBJC_ASSOCIATION_COPY);
}

-(FieldEventsBlock)CX_TextViewEventsBlock{
    return objc_getAssociatedObject(self, &cx_textVieweventsBlock);
}

-(void)cx_eventsChangeByHandler:(void (^)(void))block{
    self.CX_TextViewEventsBlock = block;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged:)
                                                name:UITextViewTextDidChangeNotification
                                              object:self];
}

-(void)textViewEditChanged:(NSNotification *)obj{
    UITextView *textView = (UITextView *)obj.object;
    NSString *toBeString = textView.text;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        NSLog(@"max = %ld",self.max);
        if (toBeString.length > self.max)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.max];
            if (rangeIndex.length == 1)
            {
                textView.text = [toBeString substringToIndex:self.max];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.max)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
            if (textView.text.length > 0) {
                self.PlaceHolder.hidden = YES;
            }
            else{
                self.PlaceHolder.hidden = NO;
            }
            
        }
        if (self.CX_TextViewEventsBlock) {
            self.CX_TextViewEventsBlock();
        }
    }
    if (textView.text.length > 0) {
        self.PlaceHolder.hidden = YES;
    }
    else{
        NSLog(@"%@",self.PlaceHolder);
        self.PlaceHolder.hidden = NO;
        [self scrollsToTop];
    }
}
@end
